package code.rest

import com.baidu.ueditor.ConfigManager
import net.liftweb.http.provider.servlet.{HTTPServletContext, HTTPRequestServlet}
import net.liftweb.http.{LiftResponse, JsonResponse, S, LiftRules}
import net.liftweb.http.rest.RestHelper
import net.liftweb.common._
import net.liftweb.util.Helpers
import org.json.JSONObject

import scala.io.Source
import scala.xml.Text
import com.baidu.ueditor.define.ActionMap;
import javax.servlet.http.HttpServletRequest

import net.liftweb.json.JsonAST._
import net.liftweb.json.JsonDSL._
import net.liftweb.json._

/**
 * Created by khb on 2014/10/29.
 */
object UploadRest extends RestHelper{

  def realPathWithContext(f: String): Box[String] = Box.asA[HTTPServletContext](LiftRules.context).map(_.ctx.getRealPath(f)).filterNot(_ eq null)

  def servletRequest: Box[HttpServletRequest] = for {
    req <- S.request
    inner <- Box.asA[HTTPRequestServlet](req.request)
  } yield inner.req

  def init() : Unit = {
    LiftRules.statelessDispatch.append(UploadRest)
  }

  serve{
    case "api" ::"controller" :: Nil Get req => {
      val actionType = req.param("action" ).openOr("")
      val  actionCode = ActionMap.getType( actionType )

      var result =""
      servletRequest.foreach { r =>
        result=new JSONObject(Helpers.readWholeStream(getClass.getResourceAsStream("/config.json"))).toString
        println(result)
      }

      val response: Box[LiftResponse] = for {
        jsonString <- S.param("q")
      } yield {
        JsonResponse(parse(result))
      }

      response
    }
  }

}
